﻿using hpcpdi.DataAccess.Base;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace hpcpdi.Infrastructure
{
    public class CustomExceptionFilter : IExceptionFilter
    {
        private readonly ILogger _logger;
        public CustomExceptionFilter(ILogger<CustomExceptionFilter> logger)
        {
            _logger = logger;
        }
        public void OnException(ExceptionContext context)
        {
            context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            context.HttpContext.Response.ContentType = "application/json";
            var ex = context.Exception;
            if (ex != null)
            {
                _logger.LogError(ex,$"程序异常：{ex.Message}");
                context.Result = new JsonResult(ResultModel.Fail<object>(null, $"程序异常"));


            }
        }
    }
}
